.TH std::abs,std::labs,std::llabs,std::imaxabs 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::abs,std::labs,std::llabs,std::imaxabs \- std::abs,std::labs,std::llabs,std::imaxabs

.SH Synopsis
   Defined in header <cstdlib>
   Defined in header <cmath>
   int       abs( int num );                   \fB(1)\fP (constexpr since C++23)
   long      abs( long num );                  \fB(2)\fP (constexpr since C++23)
   long long abs( long long num );             \fB(3)\fP \fI(since C++11)\fP
                                                   (constexpr since C++23)
   Defined in header <cstdlib>
   long       labs( long num );                \fB(4)\fP (constexpr since C++23)
   long long llabs( long long num );           \fB(5)\fP \fI(since C++11)\fP
                                                   (constexpr since C++23)
   Defined in header <cinttypes>
   std::intmax_t abs( std::intmax_t num );     \fB(6)\fP \fI(since C++11)\fP
                                                   (constexpr since C++23)
   std::intmax_t imaxabs( std::intmax_t num ); \fB(7)\fP \fI(since C++11)\fP
                                                   (constexpr since C++23)

   Computes the absolute value of the integer number num. The behavior is undefined if
   the result cannot be represented by the return type.

   If std::abs is called with an unsigned integral argument that cannot be converted to
   int by integral promotion, the program is ill-formed.

   Overload \fB(6)\fP of std::abs for std::intmax_t is provided in <cinttypes>  \fI(since C++11)\fP
   if and only if std::intmax_t is an extended integer type.

.SH Parameters

   num - integer value

.SH Return value

   The absolute value of num (i.e. |num|), if it is representable.

.SH Notes

   In 2's complement systems, the absolute value of the most-negative value is out of
   range, e.g. for 32-bit 2's complement type int, INT_MIN is -2147483648, but the
   would-be result 2147483648 is greater than INT_MAX, which is 2147483647.

.SH Example


// Run this code

 #include <climits>
 #include <cstdlib>
 #include <iostream>

 int main()
 {
     std::cout << std::showpos
               << "abs(+3) = " << std::abs(3) << '\\n'
               << "abs(-3) = " << std::abs(-3) << '\\n';

 //  std::cout << std::abs(INT_MIN); // undefined behavior on 2's complement systems
 }

.SH Output:

 abs(+3) = +3
 abs(-3) = +3

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

      DR    Applied to         Behavior as published              Correct behavior
   LWG 2192 C++98      overloads of std::abs were             declared these overloads
                       inconsistently declared in two headers in both headers

.SH See also

   abs(float)
   fabs
   fabsf              absolute value of a floating point value (\\(\\small{|x|}\\)|x|)
   fabsl              \fI(function)\fP
   \fI(C++11)\fP
   \fI(C++11)\fP
   abs(std::complex)  returns the magnitude of a complex number
                      \fI(function template)\fP
   abs(std::valarray) applies the function abs to each element of valarray
                      \fI(function template)\fP
   C documentation for
   abs,
   labs,
   llabs
